package com.cubeofnine.sugar.service;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DatabaseHelper extends SQLiteOpenHelper {
	public static final String DB_NAME = "DB_SUGAR_CRM.db";
	public static final int DB_VERSION = 2;
	public static final String TABLE_NAME = "LOGIN";
	public static final String COLUMN_ID = "_ID";
	public static final String COLUMN_USERNAME = "USERNAME";
	public static final String COLUMN_PASSWORD = "PASSWORD";
	public static final String COLUMN_SUGARCRM_URL = "SUGARCRM_URL";
	public static final String COLUMN_REMEMBER_PASSWORD = "REMEMBER_PASSWORD";
	public static final String COLUMN_AUTOMATIC_LOGIN = "AUTOMATIC_LOGIN";
	public static final String COLUMN_SESSION_ID = "SESSION_ID";
	public static final String COLUMN_IS_ADMIN = "IS_ADMIN";

	public DatabaseHelper(Context context) {
		super(context, DB_NAME, null, DB_VERSION);
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		String sql = "CREATE TABLE IF NOT EXISTS " + TABLE_NAME + " " + " ("
				+ COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT " + ","
				+ COLUMN_USERNAME + " VARCHAR(60) DEFAULT NULL " + ","
				+ COLUMN_PASSWORD + " VARCHAR(60) DEFAULT NULL " + ","
				+ COLUMN_SUGARCRM_URL + " VARCHAR(500) DEFAULT NULL " + ","
				+ COLUMN_REMEMBER_PASSWORD + " VARCHAR(1) DEFAULT 'N' " + ","
				+ COLUMN_AUTOMATIC_LOGIN + " VARCHAR(1) DEFAULT 'N' " + ","
				+ COLUMN_SESSION_ID + " VARCHAR(50) DEFAULT NULL " + ","
				+ COLUMN_IS_ADMIN + " VARCHAR(1) DEFAULT 'N');";
		db.execSQL(sql);
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int databaseOldVersion,
			int databaseNewVersion) {
		db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
		onCreate(db);
	}

	public Cursor select() {
		SQLiteDatabase db = this.getReadableDatabase();
		Cursor cursor = null;

		cursor = db.query(TABLE_NAME, new String[] { COLUMN_ID, // 0
				COLUMN_USERNAME, // 1
				COLUMN_PASSWORD, // 2
				COLUMN_SUGARCRM_URL, // 3
				COLUMN_REMEMBER_PASSWORD, // 4
				COLUMN_AUTOMATIC_LOGIN, // 5
				COLUMN_SESSION_ID, // 6
				COLUMN_IS_ADMIN // 6
				}, null, null, null, null, null);

		return cursor;
	}

	public boolean insert(String username, String password, String sugarcrmURL,
			String rememberPassword, String automaticLogin, String sessionId,
			String isAdmin) {
		SQLiteDatabase db = this.getWritableDatabase();
		boolean status = false;

		ContentValues contentValues = new ContentValues();
		contentValues.put(COLUMN_USERNAME, username);
		contentValues.put(COLUMN_PASSWORD, password);
		contentValues.put(COLUMN_SUGARCRM_URL, sugarcrmURL);
		contentValues.put(COLUMN_REMEMBER_PASSWORD, rememberPassword);
		contentValues.put(COLUMN_AUTOMATIC_LOGIN, automaticLogin);
		contentValues.put(COLUMN_SESSION_ID, sessionId);
		contentValues.put(COLUMN_IS_ADMIN, isAdmin);
		status = db.insert(TABLE_NAME, COLUMN_USERNAME, contentValues) == 1;

		db.close();
		return status;
	}

	public boolean update(int id, String username, String password,
			String sugarcrmURL, String rememberPassword, String automaticLogin,
			String sessionId, String isAdmin) {
		SQLiteDatabase db = this.getWritableDatabase();
		boolean status = false;

		ContentValues contentValues = new ContentValues();
		contentValues.put(COLUMN_USERNAME, username);
		contentValues.put(COLUMN_PASSWORD, password);
		contentValues.put(COLUMN_SUGARCRM_URL, sugarcrmURL);
		contentValues.put(COLUMN_REMEMBER_PASSWORD, rememberPassword);
		contentValues.put(COLUMN_AUTOMATIC_LOGIN, automaticLogin);
		contentValues.put(COLUMN_SESSION_ID, sessionId);
		contentValues.put(COLUMN_IS_ADMIN, isAdmin);
		status = db.update(TABLE_NAME, contentValues, COLUMN_ID + " = " + id,
				null) == 1;

		db.close();
		return status;
	}

	public boolean updateAutomaticLogin(String automaticLogin) {
		SQLiteDatabase db = this.getWritableDatabase();
		boolean status = false;

		ContentValues contentValues = new ContentValues();
		contentValues.put(COLUMN_AUTOMATIC_LOGIN, automaticLogin);
		status = db.update(TABLE_NAME, contentValues, null, null) == 1;

		db.close();
		return status;
	}

	public boolean delete(int id) {
		SQLiteDatabase db = this.getWritableDatabase();
		boolean status = false;

		if (id == -1) {
			status = db.delete(TABLE_NAME, null, null) >= 1;
		} else {
			status = db.delete(TABLE_NAME, COLUMN_ID + " = " + id, null) == 1;
		}

		db.close();
		return status;
	}
}
